package com.example.wx.service.convertor;

import com.example.wx.common.SysConstant;
import com.example.wx.common.enums.WxPublicStatusEnum;
import com.example.wx.dao.entity.WxPublicAccountUserDO;
import com.example.wx.dao.entity.WxPublicMainDO;
import com.example.wx.dao.entity.WxPublicMainExtendDO;
import com.example.wx.dao.entity.WxPublicMessageTemplateDO;
import com.example.wx.modle.accessToken.AccessToken;
import com.example.wx.modle.message.WxMessageTemplateDTO;
import com.example.wx.modle.req.WxPublicMainAddReqDTO;
import com.example.wx.other.SnowIdUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class WxPublicMainConvertor {

    public static WxPublicMainExtendDO buildWxPublicMainDO(WxPublicMainAddReqDTO wxPublicMainAddReqDTO, AccessToken accessToken) {
        WxPublicMainExtendDO wxPublicMainExtendDO = new WxPublicMainExtendDO();
        wxPublicMainExtendDO.setId(SnowIdUtils.uniqueLong());
        wxPublicMainExtendDO.setWxPublicAppId(wxPublicMainAddReqDTO.getWxPublicAppId());
        wxPublicMainExtendDO.setWxPublicAppSecret(wxPublicMainAddReqDTO.getWxPublicAppSecret());
        wxPublicMainExtendDO.setWxPublicName(wxPublicMainAddReqDTO.getWxPublicName());
        wxPublicMainExtendDO.setSalesAreaBranchId(wxPublicMainAddReqDTO.getSalesAreaBranchId());
        wxPublicMainExtendDO.setSalesAreaName(wxPublicMainAddReqDTO.getSalesAreaName());
        wxPublicMainExtendDO.setStatus(WxPublicStatusEnum.ENABLE.getCode());
        wxPublicMainExtendDO.setWxAccessToken(accessToken.getAccess_token());
        wxPublicMainExtendDO.setWxAccessTokenExpire(accessToken.getExpires_in());
        wxPublicMainExtendDO.setCreateBy(null);
        wxPublicMainExtendDO.setCreateCode(null);
        wxPublicMainExtendDO.setCreateName(null);
        wxPublicMainExtendDO.setCreateTime(LocalDateTime.now());
        wxPublicMainExtendDO.setUpdateBy(null);
        wxPublicMainExtendDO.setUpdateCode(null);
        wxPublicMainExtendDO.setUpdateName(null);
        wxPublicMainExtendDO.setUpdateTime(LocalDateTime.now());
        return wxPublicMainExtendDO;
    }

    public static WxPublicAccountUserDO buildWxPublicAccountUserDO(String appId, String openId) {
        WxPublicAccountUserDO wxPublicAccountUserDO = new WxPublicAccountUserDO();
        wxPublicAccountUserDO.setId(SnowIdUtils.uniqueLong());
        wxPublicAccountUserDO.setWxPublicAppId(appId);
        wxPublicAccountUserDO.setWxOpenId(openId);
        wxPublicAccountUserDO.setUserId(0L);
        wxPublicAccountUserDO.setUserCode("");
        wxPublicAccountUserDO.setUserName("");
        wxPublicAccountUserDO.setCreateBy(0L);
        wxPublicAccountUserDO.setCreateCode("");
        wxPublicAccountUserDO.setCreateName("");
        wxPublicAccountUserDO.setCreateTime(LocalDateTime.now());
        wxPublicAccountUserDO.setUpdateBy(0L);
        wxPublicAccountUserDO.setUpdateCode("");
        wxPublicAccountUserDO.setUpdateName("");
        wxPublicAccountUserDO.setUpdateTime(LocalDateTime.now());
        return wxPublicAccountUserDO;
    }

    public static List<WxPublicMainExtendDO> buWxPublicMainExtendDOList(List<WxPublicMainDO> wxPublicMainDOS, WxPublicStatusEnum wxPublicStatusEnum) {
        return wxPublicMainDOS.stream().map(x -> {
            WxPublicMainExtendDO wxPublicMainExtendDO = new WxPublicMainExtendDO();
            wxPublicMainExtendDO.setStatus(wxPublicStatusEnum.getCode());
            wxPublicMainExtendDO.setId(x.getId());
            wxPublicMainExtendDO.setUpdateBy(0L);
            wxPublicMainExtendDO.setUpdateCode("");
            wxPublicMainExtendDO.setUpdateName("");
            wxPublicMainExtendDO.setUpdateTime(LocalDateTime.now());
            return wxPublicMainExtendDO;
        }).collect(Collectors.toList());
    }

    public static List<WxPublicMainExtendDO> buWxPublicMainExtendDOList(List<WxPublicMainDO> wxPublicMainDOS) {
        return wxPublicMainDOS.stream().map(x -> {
            WxPublicMainExtendDO wxPublicMainExtendDO = new WxPublicMainExtendDO();
            wxPublicMainExtendDO.setDeleteFlag(SysConstant.DELETE_FLAG_YES);
            wxPublicMainExtendDO.setId(x.getId());
            wxPublicMainExtendDO.setUpdateBy(0L);
            wxPublicMainExtendDO.setUpdateCode("");
            wxPublicMainExtendDO.setUpdateName("");
            wxPublicMainExtendDO.setUpdateTime(LocalDateTime.now());
            return wxPublicMainExtendDO;
        }).collect(Collectors.toList());
    }

    public static List<WxPublicMessageTemplateDO> buildWxPublicMessageTemplateDO(List<WxMessageTemplateDTO.TemplateList> templateList, String wxPublicAppId) {
        return Optional.ofNullable(templateList).orElseGet(ArrayList::new).stream().map(x->{
            WxPublicMessageTemplateDO wxPublicMessageTemplateDO = new WxPublicMessageTemplateDO();
            wxPublicMessageTemplateDO.setId(SnowIdUtils.uniqueLong());
            wxPublicMessageTemplateDO.setWxPublicAppId(wxPublicAppId);
            wxPublicMessageTemplateDO.setWxTemplateId(x.getTemplate_id());
            wxPublicMessageTemplateDO.setVkTemplateId("");
            wxPublicMessageTemplateDO.setWxTemplateTitle(x.getTitle());
            wxPublicMessageTemplateDO.setWxTemplateContent(x.getContent());
            wxPublicMessageTemplateDO.setDeleteFlag(0);
            wxPublicMessageTemplateDO.setCreateBy(null);
            wxPublicMessageTemplateDO.setCreateCode(null);
            wxPublicMessageTemplateDO.setCreateName(null);
            wxPublicMessageTemplateDO.setCreateTime(LocalDateTime.now());
            wxPublicMessageTemplateDO.setUpdateBy(null);
            wxPublicMessageTemplateDO.setUpdateCode(null);
            wxPublicMessageTemplateDO.setUpdateName(null);
            wxPublicMessageTemplateDO.setUpdateTime(LocalDateTime.now());
            return wxPublicMessageTemplateDO;
        }).collect(Collectors.toList());
    }
}
